package cn.iot.sys.service.impl;

import cn.iot.common.dto.ResultMsg;
import cn.iot.common.utils.StringUtils;
import cn.iot.sys.dao.SysEwDeviceDao;
import cn.iot.sys.entity.SysEwDevice;
import cn.iot.sys.service.SysEwDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysEwDeviceServiceImpl extends ServiceImpl<SysEwDeviceDao, SysEwDevice> implements SysEwDeviceService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public ResultMsg addEwDevice(SysEwDevice sysEwDevice) {
        logger.info("SysEwDeviceServiceImpl addEwDevice sysEwDevice{}",sysEwDevice);
        sysEwDevice.setCreateTime(new Date());
        int count = this.baseMapper.insert(sysEwDevice);
        logger.debug("SysEwDeviceServiceImpl addEwDevice count{}",count);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",null);
    }

    @Override
    public ResultMsg updEwDevice(SysEwDevice sysEwDevice) {
        logger.info("SysEwDeviceServiceImpl updEwDevice sysEwDevice{}",sysEwDevice);
        sysEwDevice.setModifyTime(new Date());
        int count = this.baseMapper.updateById(sysEwDevice);
        logger.debug("SysEwDeviceServiceImpl updEwDevice count{}",count);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",null);
    }

    @Override
    public ResultMsg delEwDeviceById(int id) {
        logger.info("SysEwDeviceServiceImpl delEwDeviceById id{}",id);
        int count = this.baseMapper.deleteById(id);
        logger.debug("SysEwDeviceServiceImpl delEwDeviceById count{}",count);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",null);
    }

    @Override
    public ResultMsg getEwDeviceList(Map<String, Object> map) {
        logger.info("SysEwDeviceServiceImpl getEwDeviceList map{}",map);
        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        String gatewayId = (String) map.get("gatewayId");
        String deviceName = (String) map.get("deviceName");
        String location = (String) map.get("location");
        QueryWrapper<SysEwDevice> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(gatewayId)){
            queryWrapper.eq("gateway_id",gatewayId);
        }
        if (StringUtils.isNotEmpty(deviceName)){
            queryWrapper.eq("device_name",deviceName);
        }
        if (StringUtils.isNotEmpty(location)){
            if (location.contains("/")){
                queryWrapper.eq("device_location",location);
            }else {
                queryWrapper.like("device_location",location);
            }
        }
        Page<SysEwDevice> page = new Page<>(pageNum,pageSize);
        Page<SysEwDevice> page1 = this.baseMapper.selectPage(page, queryWrapper);
        List<SysEwDevice> sysEwDeviceList = page1.getRecords();
        int count = this.baseMapper.selectCount(queryWrapper);
        int pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        map.put("sysEwDeviceList",sysEwDeviceList);
        map.put("pageCount",pageCount);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        logger.debug("SysEwDeviceServiceImpl getEwDeviceList msg{}",msg);
        return msg;
    }
}
